# -*- coding: utf-8 -*- 

import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    for group_info in today_groups:
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)
    
    sold_total = soup.findAll('span', attrs={'id':'paid_number'})[0].getText()
        
    group_info.sold_total = int(sold_total)
    group_info.put()
        
    logging.info("groupinfo updated succeed : %s"%group_info.link)
        

def get_site():
    # create group site
    group_site = GroupSite.get_by_key_name("SITE_sohu")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_sohu")
        group_site.code = "sohu"
        group_site.name = "搜狐爱家团".decode('utf8')
        group_site.link = db.Link("http://tuan.sohu.com/beijing/life/")
        group_site.pop = 70
        group_site.put()
    
    return group_site

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos

    if today_groups.count() == 0:
        
        # get site content
        page = urllib2.urlopen(group_site.link)
        
        # soup
        soup = BeautifulSoup(page)
        
        main_div = soup.findAll('div', attrs={'class':'blockLB'})[0]
        picture = main_div.findAll('div', attrs={'class':'boxA_r'})[0].findAll('img')[0]['src']
        title = main_div.findAll('div', attrs={'class':'jttg'})[0].findNext('h2').getText()
        title = title.replace("\"", "")
        link = main_div.findAll('div', attrs={'class':'boxA_l'})[0].findAll('a')[0]['href'][14:]
        link = "http://tuan.sohu.com/beijing/life/tuan/%s.html"%link
        current_price = main_div.findAll('div', attrs={'class':'fc'})[0].findAll('h3')[0].getText()
        original_price = main_div.findAll('div', attrs={'class':'boxA_l'})[0].findAll('li', attrs={'class':'i2'})[0].findAll('p')[0].getText()[1:]
        discount = main_div.findAll('div', attrs={'class':'boxA_l'})[0].findAll('li', attrs={'class':'i2'})[0].findNext('li').findAll('p')[0].getText()
        sold_total = main_div.findAll('span', attrs={'id':'paid_number'})[0].getText()
        
        # create group_info
        group_info = GroupInfo()
        group_info.site = group_site
        group_info.title = title
        group_info.current_price = float(current_price)
        #group_info.original_price = float(original_price)
        group_info.discount = float(discount)
        group_info.picture = db.Link(picture)
        group_info.link = db.Link(link)
        group_info.sold_total = int(sold_total)
        group_info.create()
        
        group_infos.append(group_info)
        
        
        # side
        side_div = soup.findAll('div', attrs={'class':'blockR blockRF'})[0]
        link = "http://tuan.sohu.com/%s"%side_div.findAll('a')[1]['href']
        title = side_div.findAll('a')[1].getText()
        picture = side_div.findAll('img')[0]['src']
        current_price = side_div.findAll('h5')[0].findAll('span')[0].getText()[4:]
        original_price = side_div.findAll('h5')[0].findAll('del')[0].getText()[1:]
  
        group_info = GroupInfo()
        group_info.site = group_site
        group_info.title = title
        group_info.current_price = float(current_price)
        group_info.original_price = float(original_price)
        #group_info.discount = float(discount)
        group_info.picture = db.Link(picture)
        group_info.link = db.Link(link)
        #group_info.sold_total = int(sold_total)
        group_info.create()
        
        group_infos.append(group_info)  
  
    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


